package org.yanghaokun.user.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yanghaokun.basic.exception.BusinessException;
import org.yanghaokun.basic.util.AjaxResult;
import org.yanghaokun.user.dto.LoginDto;
import org.yanghaokun.user.service.ILoginInfoService;

import java.util.Map;

@RestController
@RequestMapping("/login")
@Api(value = "登录信息接口",description = "登录信息接口详细描述")
public class LoginController {

    @Autowired
    private ILoginInfoService loginInfoService;

    @PostMapping("/account")
    public AjaxResult loginInfoAccount(@RequestBody LoginDto loginDto){
        try {
            //不仅要校验，还有返回值，所以在里面直接封装AjaxResult更好
            return loginInfoService.loginAccount(loginDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误" + e.getMessage());
        }
    }

    @PostMapping("/wechat")
    public AjaxResult loginWechat(@RequestBody Map<String,String> prams){
        try {
            //不仅要校验，还有返回值，所以在里面直接封装AjaxResult更好
            return loginInfoService.loginWechat(prams);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误" + e.getMessage());
        }
    }

    //一定情况下Map能够代替类使用
    @PostMapping("/sendsms")
    public AjaxResult sendSms(@RequestBody Map<String,String> params) {
        //从床过来的参数中获取电话信息
        String phone = params.get("phone");
        String type = params.get("type");//用来区分是绑定还是注册
        try {
            loginInfoService.sendSms(params);
            //发送失败
            return AjaxResult.me();

        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("发送失败" + e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误" + e.getMessage());
        }
    }

    @PostMapping("/binder/wechat")
    public AjaxResult binderWechat(@RequestBody Map<String,String> prams){
        try {
            //不仅要校验，还有返回值，所以在里面直接封装AjaxResult更好
            return loginInfoService.binderWechat(prams);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误" + e.getMessage());
        }
    }
}
